package cc.higou.service;

import cc.higou.core.pojo.Color;
import cc.higou.core.pojo.Product;
import cc.higou.core.tools.Encoding;
import cc.higou.core.tools.PageHelper;
import org.apache.solr.client.solrj.SolrServerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 说明：
 * 后台商品的控制器
 *
 * @author:dongxing
 * @time:--
 */
@Controller
public class ProductController {
    @Autowired
    private ProductService productService;


    private final static Logger LOGGER = LoggerFactory.getLogger(ProductController.class);

    //product的跳转
    @RequestMapping(value = "/console/product/{pageName}.do")
    public String centerProductShow(@PathVariable("pageName") String pageName) {

        return "product/" + pageName;
    }

    //显示所有商品的
    @RequestMapping(value = "/console/product/list.do")
    public String centerProductListShow(Model model, Product product, Integer pageNum, Integer pageSize) {
        //处理给请求乱码的问题
        String name = Encoding.encodeGetRequest(product.getName());
        product.setName(name);
        PageHelper.Page<Product> page = productService.findByExample(product, pageNum, pageSize);
        model.addAttribute("page", page);
        model.addAttribute("name", product.getName());
        model.addAttribute("brandId", product.getBrandId());
        model.addAttribute("isShow", product.getIsShow());
        return "product/list";
    }

    //删除商品
    @RequestMapping(value = "/console/product/doDelete.do")
    public String centerDelete(String ids, Product product, Integer pageNum) throws UnsupportedEncodingException {


        String[] strings = ids.split(",");
        if (strings.length > 0) {
            for (String id : strings) {
                //删除
                productService.deleteById(id);
            }
        }
        String name = product.getName() == null ? "" : product.getName();
        name = new String(name.getBytes("UTF-8"), "iso-8859-1");
        String brandId = product.getBrandId() == null ? "" : product.getBrandId() + "";
        String isShow = product.getIsShow() == null ? "" : product.getIsShow() + "";

        return "redirect:list.do?name=" + name + "&brandId=" + brandId + "&isShow=" + isShow + "&pageNum=" + pageNum;
    }


    //查询商品的颜色
    @RequestMapping(value = "/console/product/showAdd.do")
    public String centerShowColors(Model model) {

        List<Color> colors = productService.findEnableColors();
        model.addAttribute("colors", colors);

        return "product/add";
    }


    //添加商品的信息
    @RequestMapping(value = "/console/product/doAdd.do")
    public String saveProducts(Product product) {
        productService.add(product);
        return "redirect:list.do";
    }


    //上下架商品
    @RequestMapping(value = "/console/product/isShow.do")
    public String centerShowProducts(Integer flag,String ids, Product product, Integer pageNum) throws IOException, SolrServerException {
        Product product1 = new Product();
        product1.setIsShow(flag);
        productService.update(product1,ids);
        //跳转列表的查询页面
        String name = product.getName() == null ? "" : product.getName();
        name = new String(name.getBytes("UTF-8"), "iso-8859-1");
        String brandId = product.getBrandId() == null ? "" : product.getBrandId() + "";
        String isShow = product.getIsShow() == null ? "" : product.getIsShow() + "";
        return "redirect:list.do?name=" + name + "&brandId=" + brandId + "&isShow=" + isShow + "&pageNum=" + pageNum;
    }




    //修改页面的跳转
    @RequestMapping(value = "/console/product/showEdit.do")
    public String centerEditProduct(Long productId, Model model) {
        LOGGER.info("开始执行修改页面的额方法");
        Product product = productService.findProductById(productId);
        model.addAttribute("product", product);
        //查询颜色
        List<Color> colors = productService.findEnableColors();
        model.addAttribute("colors", colors);



        if (product != null) {
            return "/product/edit";
        }
        return null;
    }

}
